package com.chinatelecom.check.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinatelecom.check.dao.CheckTableMapper;
import com.chinatelecom.check.entity.CheckTable;
import com.chinatelecom.check.model.CheckTableModel;
import com.chinatelecom.check.model.CheckTableQuestionModel;
import com.chinatelecom.check.service.CheckTableQuestionService;
import com.chinatelecom.check.service.CheckTableService;
import com.chinatelecom.framework.generic.GenericServiceImpl;
import com.chinatelecom.framework.model.PageData;
import com.chinatelecom.framework.model.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * CheckTable服务实现
 */
@Service
@Transactional
public class CheckTableServiceImpl extends GenericServiceImpl<CheckTable> implements CheckTableService{
	
	@Autowired
	private CheckTableMapper mapper;

	@Autowired
	private CheckTableQuestionService CheckTableQuestionService;
	
	@Override
	public CheckTableModel getDetail(Integer id) {
		CheckTableModel model = new CheckTableModel();
		CheckTable record = mapper.selectById(id);
		BeanUtil.copyProperties(record, model);
		model.setQuestionList(CheckTableQuestionService.getList(id));
		return model;
	}
	
	@Override
	public PageData<CheckTableModel> page(PageParam pageParam, CheckTableModel businessParam) {
		QueryWrapper<CheckTable> wrapper = new QueryWrapper<>();
		if(StrUtil.isNotEmpty(businessParam.getName())){
			wrapper.like("name", businessParam.getName());
		}
		if(StrUtil.isNotEmpty(businessParam.getCode())){
			wrapper.like("code", businessParam.getCode());
		}
		IPage<CheckTable> page = new Page<>(pageParam.getPage(), pageParam.getPageSize());
		IPage<CheckTable> list = this.getPage(page, wrapper);

		PageData<CheckTable> data = new PageData<>(list, pageParam);
		List<CheckTableModel> mlist = data.getList().stream().map(a -> {
			CheckTableModel dto = new CheckTableModel();
			BeanUtils.copyProperties(a, dto);
			return dto;
		}).collect(Collectors.toList());

		PageData<CheckTableModel> result = new PageData<CheckTableModel>();
		BeanUtils.copyProperties(data, result);
		result.setList(mlist);
		return result;
	}


}
